import UIAbility from '@ohos.app.ability.UIAbility';
import hilog from '@ohos.hilog';
import window from '@ohos.window';
import { initialize } from '@hw-agconnect/hmcore';
import util from '@ohos.util';

export default class EntryAbility extends UIAbility {
  async onCreate(want, launchParam) {
    try {
      let input = await this.context.resourceManager
        .getRawFileContent('agconnect-services.json')
      let jsonString  = util.TextDecoder.create('utf-8',
        {ignoreBOM: true}).decodeWithStream(input, {stream: false});
      initialize(this.context, JSON.parse(jsonString));
      console.info('HMLog--->','EntryAbility.onCreate.ok')
    }catch (err){
      console.error('HMLog--->','EntryAbility.onCreate.Err:'+JSON.stringify(err))
    }

  }

  onDestroy() {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy');
  }

  onWindowStageCreate(windowStage: window.WindowStage) {
    // Main window is created, set main page for this ability
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');

    windowStage.loadContent('pages/IndexPage', (err, data) => {
      if (err.code) {
        hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
        return;
      }
      hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
    });
  }

  onWindowStageDestroy() {
    // Main window is destroyed, release UI related resources
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
  }

  onForeground() {
    // Ability has brought to foreground
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground');
  }

  onBackground() {
    // Ability has back to background
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground');
  }
}
